<?php

namespace Api\Model;

use Think\Model;

class UsersModel extends Model {

    function addUser($username, $password, $nickname, $salt, $deviceid, $group) {
        $data = array(
            'username' => $username,
            'password' => $password,
            'nickname' => $nickname,
            'salt' => $salt,
            'deviceid' => $deviceid,
            'group' => $group,
            'createtime' => date('Y-m-d H:i:s', time())
        );
        return $this->data($data)->add();
    }

    function upUser($user_id, $data) {
        $where = array('id' => $user_id);
        return $this->where($where)->save($data);
    }

    function findUserbyId($id) {
        $where = array('id' => $id);
        return $this->where($where)->limit(1)->find();
    }

    function getUserinfobyuserid($user_id) {
        $where = array('jl_users.id' => $user_id);
        $field = 'jl_users.id as userid,jl_users.username as username,jl_users.nickname as nickname,jl_users.email as email,'
                . 'jl_users.group as user_group,jl_users.createtime as createtime,'
                . 'jl_userinfo.user_tel as telphone,jl_userinfo.user_gender as gender,jl_userinfo.user_aname as truename';
        return $this->join('LEFT JOIN __USERINFO__ on __USERS__.id =__USERINFO__.user_id')->where($where)->field($field)->find();
    }

    function findUserbyUsername($username) {
        $where = array(
            'username' => $username
        );
        return $this->where($where)->find();
    }

}
